/**
 * Copyright (c) 2013-Now http://dreamcode123321.com All rights reserved.
 */
package com.jeesite.modules.ab.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ab.entity.AbNews;
import com.jeesite.modules.ab.service.AbNewsService;

import java.util.List;

/**
 * 新闻Controller
 * @author dc
 * @version 2020-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/ab/abNews")
public class AbNewsController extends BaseController {

	@Autowired
	private AbNewsService abNewsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AbNews get(String newsCode, boolean isNewRecord) {
		return abNewsService.get(newsCode, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ab:abNews:view")
	@RequestMapping(value = {"list", ""})
	public String list(AbNews abNews, Model model) {
		model.addAttribute("abNews", abNews);
		return "modules/ab/abNewsList";
	}
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ab:abNotiy:view")
	@RequestMapping(value = "shortListData")
	@ResponseBody
	public List<AbNews> shortListData(AbNews abNotiy, HttpServletRequest request, HttpServletResponse response) {
		List<AbNews> list = abNewsService.shortListData();
		return list;
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AbNews> listData(AbNews abNews, HttpServletRequest request, HttpServletResponse response) {
		abNews.setPage(new Page<>(request, response));
		Page<AbNews> page = abNewsService.findPage(abNews);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(AbNews abNews, Model model) {
		model.addAttribute("abNews", abNews);
		return "modules/ab/abNewsForm";
	}

	/**
	 * 保存新闻
	 */
	@RequiresPermissions("ab:abNews:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AbNews abNews) {
		abNewsService.save(abNews);
		return renderResult(Global.TRUE, text("保存新闻成功！"));
	}
	
	/**
	 * 删除新闻
	 */
	@RequiresPermissions("ab:abNews:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AbNews abNews) {
		abNewsService.delete(abNews);
		return renderResult(Global.TRUE, text("删除新闻成功！"));
	}
	
}